import { gameConfigHandle, propName } from "core/config/gameConfig";
import utils from "core/utils/utils";
import { DamageDataModel, TriggerEffectDataModel } from "core/model/battleInfoModel";
import { FighterModel, EffectModel, FighterAttriModel } from "core/model/fighterModel";
import { AngerType, HurtType, AttackType, BuffPositive, BuffType, AnimationValueType, TargetType, TeamID, TriggerTeamType, TriggerType, UnitType, BuffMainType } from "core/typeDefine/typeDefine";
import BattleHandle from "./battleHandle";
import { FighterSkillModel } from "core/model/skillModel";
import { BuffModel } from "core/model/buffModel";
import fighterHandle from "core/handle/fighterHandle";



export default class BattleSkillHandle {
    battleHandle: BattleHandle
    skillFunc: any
    constructor(battleHandle: BattleHandle) {
        this.skillFunc = {}
        this.battleHandle = battleHandle
    }
    setSkill(skillID: number, f) {
        this.skillFunc[skillID] = f
    }

    async startUseSkill(attacker: FighterModel, skill: FighterSkillModel) {
        this.battleHandle.startUseSkillAnimation(attacker.teamIndexID, skill.index, skill.cd / 1000)
        let timerID = this.battleHandle.timerHandle.setTimeout(() => {
            let mp = Math.floor(skill.mp * (1 - attacker.battleAttris.reduceMpPer / 100))
            if (attacker.unitType == UnitType.Partner) {
                if (attacker.battleAttris.mp >= mp) {
                    attacker.battleAttris.mp -= mp
                } else {
                    attacker.waitSkills.unshift(skill)
                    return
                }
            }
            this.useSkill(skill, attacker)
            this.startUseSkill(attacker, skill)
            if (skill.doubleTriggerPer > 0) {
                if (this.battleHandle.rand.intn(100) <= skill.doubleTriggerPer) {
                    this.battleHandle.timerHandle.setTimeout(() => {
                        this.useSkill(skill, attacker)
                        this.battleHandle.skillTipAnimation(attacker.teamIndexID, skill.index, "多重")
                    }, 200)

                }
            }
        }, () => {
            return skill.cd / (1 + attacker.battleAttris.skillReduceCdPer / 100)
        })
        attacker.timerObject[skill.tid] = timerID
    }

    playSkill(attacker: FighterModel) {
        for (let i = attacker.waitSkills.length - 1; i >= 0; i--) {
            let skill = attacker.waitSkills[i]
            let mp = Math.floor(skill.mp * (1 - attacker.battleAttris.reduceMpPer / 100))

            if (attacker.battleAttris.mp >= mp) {
                attacker.battleAttris.mp -= mp
                this.useSkill(skill, attacker)
                if (skill.doubleTriggerPer > 0) {
                    if (this.battleHandle.rand.intn(100) <= skill.doubleTriggerPer) {
                        this.battleHandle.timerHandle.setTimeout(() => {
                            this.useSkill(skill, attacker)
                            this.battleHandle.skillTipAnimation(attacker.teamIndexID, skill.index, "多重")
                        }, 200)
                    }
                }
                this.startUseSkill(attacker, skill)
                attacker.waitSkills.splice(i, 1)
            }
        }

    }

    async useSkill(skill: FighterSkillModel, attacker: FighterModel): Promise<any> {
        let skillItemConfig = gameConfigHandle.configByID[skill.tid]
        if (skillItemConfig.skillCd) {
            if (attacker.skillCd[skill.tid]) {
                let cd = utils.getIncValues(skillItemConfig.skillCd, skill.lvl - 1)
                if (this.battleHandle.round - attacker.skillCd[skill.tid] > cd) {
                    attacker.skillCd[skill.tid] = this.battleHandle.round
                } else {
                    return
                }
            } else {
                attacker.skillCd[skill.tid] = this.battleHandle.round
            }
        }
        let triggerEffectPreData = new TriggerEffectDataModel()
        triggerEffectPreData.skill = skill
        // triggerEffectPreData.attackType = AttackType.Skill
        triggerEffectPreData.skillID = skill.tid

        let skillConfig = gameConfigHandle.gameConfig.skill
        let skillNeedAnger = Math.floor(skillConfig.skillNeedAnger * (1 - attacker.battleAttris.reduceMpPer / 100))
        if (skillNeedAnger < skillConfig.skillNeedAngerMin) {
            skillNeedAnger = skillConfig.skillNeedAngerMin
        }

        triggerEffectPreData.angerValue = skillNeedAnger
        attacker.battleAttris.anger -= skillNeedAnger
        let saveMpPer = attacker.battleAttris.saveMpPer
        attacker.battleAttris.anger = Math.floor(attacker.battleAttris.anger * saveMpPer / 100)

        // this.battleHandle.useSkillAnimation(attacker.teamIndexID, skill.index)
        await this.triggerEffects(attacker, TriggerType.UseSkill, null, triggerEffectPreData)

        await this.triggerEffects(null, TriggerType.OtherUseSkill, attacker, triggerEffectPreData)

    }

    async triggerEffects(originTrigger: FighterModel, type: TriggerType, stakeholder: FighterModel, triggerEffectPreData: TriggerEffectDataModel) {
        let effects: EffectModel[]

        if (type >= 50) {
            effects = this.battleHandle.otherEffects[type]
        } else {
            effects = originTrigger.effects[type]
        }
        if (effects) {
            for (let i = 0; i < effects.length; i++) {
                let effect: EffectModel = effects[i]
                let effectData = effect.data
                let trigger = effect.trigger
                if (type >= 50 && trigger.teamIndexID == stakeholder.teamIndexID) {
                    continue
                }
                if (type >= 50 && trigger.battleAttris.hp < 1) {
                    continue
                }
                if (effectData.triggerPer) {
                    let triggerTypeRate = effectData.triggerPer ? utils.getIncValues(effectData.triggerPer, effect.eSkillLvl - 1) : 100
                    if (triggerTypeRate < this.battleHandle.rand.intn(100)) {
                        continue
                    }
                }

                if (effectData.triggerCondition) {
                    let triggerCondition = effectData.triggerCondition
                    if (triggerCondition.triggerAttackType && triggerEffectPreData.attackType !== triggerCondition.triggerAttackType) {
                        continue
                    }
                    if (triggerCondition.triggerHurtType) {
                        if (typeof triggerCondition.triggerHurtType == "number") {
                            if (triggerEffectPreData.hurtType !== triggerCondition.triggerHurtType) {
                                continue
                            }
                        } else {
                            if (triggerCondition.triggerHurtType.indexOf(triggerEffectPreData.hurtType) == -1) {
                                continue
                            }
                        }
                    }

                    if (triggerCondition.triggerBuffType && triggerEffectPreData.buffType !== triggerCondition.triggerBuffType) {
                        continue
                    }
                    if (triggerCondition.triggerisCrit && triggerEffectPreData.isCrit !== triggerCondition.triggerisCrit) {
                        continue
                    }
                    if (triggerCondition.triggerisHit && triggerEffectPreData.isDodge == true) {
                        continue
                    }
                    if (triggerCondition.triggerisBlock && triggerEffectPreData.isBlock != true) {
                        continue
                    }

                    if (triggerCondition.triggerTeam && stakeholder) {
                        if (triggerCondition.triggerTeam == TriggerTeamType.Friend && trigger.teamID !== stakeholder.teamID) {
                            continue
                        }
                        if (triggerCondition.triggerTeam == TriggerTeamType.Oppent && trigger.teamID == stakeholder.teamID) {
                            continue
                        }
                    }

                    if (triggerCondition.triggerThisSkill) {
                        if (effect.skillID !== triggerEffectPreData.skillID) {
                            continue
                        }
                    }

                    if (triggerCondition.triggerHpPer) {
                        if (triggerCondition.triggerHpPer <= trigger.battleAttris.hp / trigger.battleAttris.maxHp * 100) {
                            continue
                        }
                    }
                }

                if (effectData.triggerTimes && effect.triggerTimes >= effectData.triggerTimes) {
                    continue
                }
                effect.triggerTimes += 1
                if (effectData.triggerTimesInterval && effect.triggerTimes % effectData.triggerTimesInterval !== 0) {
                    //触发间隔
                    continue
                }

                let _triggerEffectData = new TriggerEffectDataModel(triggerEffectPreData)
                _triggerEffectData.type = type
                _triggerEffectData.effect = effect
                _triggerEffectData.trigger = effect.trigger
                _triggerEffectData.effectData = effectData
                _triggerEffectData.skillID = effect.skillID
                await this.triggerEvents(trigger, stakeholder, _triggerEffectData)

            }
        }
    }

    async triggerEvents(trigger: FighterModel, stakeholder: FighterModel, triggerEffectData: TriggerEffectDataModel) {
        if (trigger.unitType == UnitType.Altar) {
            this.battleHandle.useSkillAnimation(trigger.teamIndexID, triggerEffectData.effect.skillIndex)
        }
        await this.battleHandle.setBattleTimeout(150)
        for (let i = 0; i < triggerEffectData.effectData.triggerEvents.length; i++) {
            let triggerEventDataConfig = triggerEffectData.effectData.triggerEvents[i]
            await this.triggerEvent(trigger, stakeholder, triggerEffectData, triggerEventDataConfig, i)
        }

    }

    async triggerEvent(trigger: FighterModel, stakeholder: FighterModel, triggerEffectData: TriggerEffectDataModel, triggerEventDataConfig: any, effectDataIndex) {
        if (triggerEventDataConfig.eventName == "attack") { //攻击
            let defenders = []
            if (stakeholder) {
                defenders.push(stakeholder)
            }
            await this.battleHandle.attackPre(trigger, triggerEventDataConfig, triggerEffectData.effect.eSkillLvl, triggerEffectData, triggerEffectData.angerValue, defenders)
        } else if (triggerEventDataConfig.eventName == "addBuff") { //buff
            let buffID: number
            if (typeof triggerEventDataConfig.buffID == "number") {
                buffID = triggerEventDataConfig.buffID
            } else {
                buffID = utils.getOneFromArray(triggerEventDataConfig.buffID, this.battleHandle.rand)
            }

            let targetAmount = 1
            if (triggerEventDataConfig.targetAmount) {
                targetAmount = utils.getIncValues(triggerEventDataConfig.targetAmount, triggerEffectData.effect.eSkillLvl)
            }
            let fighters = this.battleHandle.getFightersByTargetType(trigger, triggerEventDataConfig.targetType, targetAmount, triggerEventDataConfig.targetRankType, stakeholder)
            for (let i = 0; i < fighters.length; i++) {
                let fighter = fighters[i]
                if (fighter) {
                    let buffConfig = gameConfigHandle.configByID[buffID]
                    let rate = 100
                    if (buffConfig.buffType == BuffType.Poison) {
                        rate = trigger.battleAttris.poisonRate - fighter.battleAttris.antiPoisonRate + 100
                    } else if (buffConfig.buffType == BuffType.Blood) {
                        rate = trigger.battleAttris.bleedRate - fighter.battleAttris.antiBleedRate + 100
                    } else if (buffConfig.buffType == BuffType.Vertigo) {
                        rate = trigger.battleAttris.stunRate - fighter.battleAttris.antiStunRate + 100
                    } else if (buffConfig.buffType == BuffType.Ice) {
                        rate = trigger.battleAttris.freezeRate - fighter.battleAttris.antiFreezeRate + 100
                    } else if (buffConfig.buffType == BuffType.Silent) {
                        rate = trigger.battleAttris.silentRate - fighter.battleAttris.antiSilentRate + 100
                    } else if (buffConfig.buffType == BuffType.Fire) {
                        rate = trigger.battleAttris.burnRate - fighter.battleAttris.antiBurnRate + 100
                    }
                    //判断是否为控制buff
                    if (buffConfig.buffType == BuffType.Vertigo || buffConfig.buffType == BuffType.Ice || buffConfig.buffType == BuffType.Silent) {
                        rate = rate + trigger.battleAttris.controlRate - fighter.battleAttris.antiControlRate
                    }

                    if (this.battleHandle.rand.intn(100) <= rate) {
                        let tempID = trigger.teamIndexID * 100000 + triggerEffectData.effect.id * 100 + effectDataIndex
                        await this.addBuff(trigger, fighter, buffID, triggerEffectData.effect.eSkillLvl, tempID, triggerEventDataConfig, triggerEffectData)
                    }
                }
            }

        } else if (triggerEventDataConfig.eventName == "addAureole") { //光环
            if (triggerEventDataConfig.targetType == TargetType.MyTeam) {
                let buffID = triggerEventDataConfig.tid
                this.addAureole(trigger, buffID, triggerEffectData.effect.eSkillLvl, trigger.teamID)
            } else if (triggerEventDataConfig.targetType == TargetType.OppentTeam) {
                let buffID = triggerEventDataConfig.tid
                this.addAureole(trigger, buffID, triggerEffectData.effect.eSkillLvl, this.battleHandle.getOpponentTeamID(trigger.teamID))
            }
        } else if (triggerEventDataConfig.eventName == "addAttris") { //添加属性
            triggerEventDataConfig.forEach(attriItem => {
                let attriName = attriItem.attriName
                let attriValue = utils.getIncValues(attriItem.attriValue, triggerEffectData.effect.eSkillLvl)
                let round = 0
                if (attriItem.round) {
                    round = this.battleHandle.round + attriItem.round
                }
                trigger.tempAttris.push({ name: attriName, value: attriValue, round: round })
            })
        } else if (triggerEventDataConfig.eventName == "recover") { //恢复
            if (triggerEventDataConfig.attriName == 'hp') {
                let value = 0
                if (triggerEventDataConfig.hpPer) { //恢复生命
                    let recoverPer = utils.getIncValues(triggerEventDataConfig.hpPer, triggerEffectData.effect.eSkillLvl)
                    value += Math.floor(recoverPer * trigger.battleAttris.maxHp / 100)
                }
                if (triggerEventDataConfig.attackPer) { //根据攻击力恢复生命
                    let recoverPer = utils.getIncValues(triggerEventDataConfig.attackPer, triggerEffectData.effect.eSkillLvl)
                    value = Math.floor(recoverPer * trigger.battleAttris.attack / 100)
                }
                this.battleHandle.addHp(trigger, value, AnimationValueType.RecoverHp)
            } else if (triggerEventDataConfig.attriName == 'anger') {  //恢复怒气
                let anger = utils.getIncValues(triggerEventDataConfig.value, triggerEffectData.effect.eSkillLvl)
                trigger.battleAttris.anger += anger
                let battleConfig = gameConfigHandle.gameConfig.battle
                if (trigger.battleAttris.anger > battleConfig.maxAnger) {
                    trigger.battleAttris.anger = battleConfig.maxAnger
                }
                this.battleHandle.recoverAnimation(trigger.teamIndexID, anger, AnimationValueType.Anger)
            } else if (triggerEventDataConfig.attriName == 'shield') {  //恢复护盾
                let cureValue = 0
                if (triggerEventDataConfig.value) { //治疗值 value
                    cureValue = utils.getIncValues(triggerEventDataConfig.value, triggerEffectData.effect.eSkillLvl)
                } else if (triggerEventDataConfig.hpPer) { //治疗百分百的生命 hpPer
                    let curePer = utils.getIncValues(triggerEventDataConfig.hpPer, triggerEffectData.effect.eSkillLvl)
                    cureValue = curePer * trigger.battleAttris.maxHp / 100
                } else if (triggerEventDataConfig.attackPer) { //治疗攻击的等量生命 attackPer
                    let curePer = utils.getIncValues(triggerEventDataConfig.attackPer, triggerEffectData.effect.eSkillLvl)
                    cureValue = Math.floor(curePer * trigger.battleAttris.attack / 100)
                }
                cureValue = Math.floor(cureValue)
                trigger.battleAttris.shield += cureValue
                if (trigger.battleAttris.shield >= trigger.battleAttris.maxHp * 1.5) {
                    trigger.battleAttris.shield = Math.floor(trigger.battleAttris.maxHp * 1.5)
                }
                this.battleHandle.recoverAnimation(trigger.teamIndexID, cureValue, AnimationValueType.CureShield)
            }
        } else if (triggerEventDataConfig.eventName == "cure") { //治疗
            await this.triggerEffects(trigger, TriggerType.CurePre, null, new TriggerEffectDataModel({ ...triggerEffectData, cureAttriName: triggerEventDataConfig.attriName }))
            let amount = triggerEventDataConfig.targetAmount ? utils.getIncValues(triggerEventDataConfig.targetAmount, triggerEffectData.effect.eSkillLvl) : 0

            let partners = this.battleHandle.getFightersByTargetType(trigger, triggerEventDataConfig.targetType, amount, triggerEventDataConfig.targetRankType, stakeholder)
            if (triggerEventDataConfig.attriName == 'hp' || !triggerEventDataConfig.attriName) { //治疗生命
                for (let i = 0; i < partners.length; i++) {
                    let fighter = partners[i]
                    let cureValue = 0
                    if (triggerEventDataConfig.value) { //治疗值 value
                        cureValue = utils.getIncValues(triggerEventDataConfig.value, triggerEffectData.effect.eSkillLvl)
                    } else if (triggerEventDataConfig.hpPer) { //治疗百分百的生命 hpPer
                        let curePer = utils.getIncValues(triggerEventDataConfig.hpPer, triggerEffectData.effect.eSkillLvl)
                        cureValue = curePer * fighter.battleAttris.maxHp / 100 * (1 + trigger.battleAttris.curePer / 100)
                    } else if (triggerEventDataConfig.triggerHpPer) { //治疗攻击的等量生命 attackPer
                        let curePer = utils.getIncValues(triggerEventDataConfig.triggerHpPer, triggerEffectData.effect.eSkillLvl)
                        cureValue = Math.floor(curePer * trigger.battleAttris.maxHp / 100)
                    } else if (triggerEventDataConfig.triggerAttackPer) { //治疗攻击的等量生命 attackPer
                        let curePer = utils.getIncValues(triggerEventDataConfig.triggerAttackPer, triggerEffectData.effect.eSkillLvl)
                        cureValue = Math.floor(curePer * trigger.battleAttris.attack / 100)
                    }
                    await this.triggerEffects(trigger, TriggerType.Cure, fighter, new TriggerEffectDataModel({ ...triggerEffectData, cureValue, cureAttriName: triggerEventDataConfig.attriName }))
                    this.battleHandle.statisticsData[trigger.teamIndexID].cure += cureValue

                    if (trigger.teamID == TeamID.Team1) {
                        this.battleHandle.damageKillData.cure[trigger.tid] = (this.battleHandle.damageKillData.cure[trigger.tid] || 0) + cureValue
                        this.battleHandle.damageKillData.cure['all'] = (this.battleHandle.damageKillData.cure['all'] || 0) + cureValue
                    }

                    this.battleHandle.addHp(fighter, cureValue, AnimationValueType.CureHp)
                    await this.triggerEffects(fighter, TriggerType.BeCure, trigger, new TriggerEffectDataModel({ ...triggerEffectData, cureValue, cureAttriName: triggerEventDataConfig.attriName }))
                }
            } else if (triggerEventDataConfig.attriName == 'anger') { //治疗怒气
                for (let i = 0; i < partners.length; i++) {
                    let fighter = partners[i]
                    if (fighter.battleAttris.hp >= 1) {
                        let cureValue = utils.getIncValues(triggerEventDataConfig.value, triggerEffectData.effect.eSkillLvl)
                        await this.triggerEffects(trigger, TriggerType.Cure, fighter, new TriggerEffectDataModel({ ...triggerEffectData, cureValue, cureAttriName: triggerEventDataConfig.attriName }))
                        fighter.battleAttris.anger += cureValue
                        if (fighter.battleAttris.anger > gameConfigHandle.gameConfig.battle.maxAnger) {
                            fighter.battleAttris.anger = gameConfigHandle.gameConfig.battle.maxAnger
                        }
                        this.battleHandle.recoverAnimation(fighter.teamIndexID, cureValue, AnimationValueType.Anger)
                        await this.triggerEffects(fighter, TriggerType.BeCure, trigger, new TriggerEffectDataModel({ ...triggerEffectData, cureValue, cureAttriName: triggerEventDataConfig.attriName }))
                    }
                }
            } else if (triggerEventDataConfig.attriName == 'shield') { //护盾
                for (let i = 0; i < partners.length; i++) {
                    let fighter = partners[i]
                    if (fighter.battleAttris.hp >= 1) {
                        let cureValue = 0
                        if (triggerEventDataConfig.value) { //治疗值 value
                            cureValue = utils.getIncValues(triggerEventDataConfig.value, triggerEffectData.effect.eSkillLvl)
                        } else if (triggerEventDataConfig.hpPer) { //治疗百分百的生命 hpPer
                            let curePer = utils.getIncValues(triggerEventDataConfig.hpPer, triggerEffectData.effect.eSkillLvl)
                            cureValue = curePer * fighter.battleAttris.maxHp / 100
                        } else if (triggerEventDataConfig.triggerHpPer) { //治疗百分百的生命 triggerHpPer
                            let curePer = utils.getIncValues(triggerEventDataConfig.triggerHpPer, triggerEffectData.effect.eSkillLvl)
                            cureValue = curePer * trigger.battleAttris.maxHp / 100
                        } else if (triggerEventDataConfig.attackPer) { //治疗攻击的等量生命 attackPer
                            let curePer = utils.getIncValues(triggerEventDataConfig.attackPer, triggerEffectData.effect.eSkillLvl)
                            cureValue = Math.floor(curePer * trigger.battleAttris.attack / 100)
                        }
                        cureValue = Math.floor(cureValue)
                        fighter.battleAttris.shield += cureValue
                        if (fighter.battleAttris.shield >= fighter.battleAttris.maxHp * 1.5) {
                            fighter.battleAttris.shield = Math.floor(fighter.battleAttris.maxHp * 1.5)
                        }
                        this.battleHandle.recoverAnimation(fighter.teamIndexID, cureValue, AnimationValueType.CureShield)
                    }
                }
            }

        } else if (triggerEventDataConfig.eventName == "hurt") { //减少生命,怒气
            let amount = triggerEventDataConfig.targetAmount ? utils.getIncValues(triggerEventDataConfig.targetAmount, triggerEffectData.effect.eSkillLvl) : 0
            let partners = this.battleHandle.getFightersByTargetType(trigger, triggerEventDataConfig.targetType, amount, triggerEventDataConfig.targetRankType, stakeholder)
            if (triggerEventDataConfig.attriName == 'hp' || !triggerEventDataConfig.attriName) { //生命
                for (let i = 0; i < partners.length; i++) {
                    let fighter = partners[i]
                    let value = 0
                    if (triggerEventDataConfig.value) { //值 value
                        value = utils.getIncValues(triggerEventDataConfig.value, triggerEffectData.effect.eSkillLvl)
                    } else if (triggerEventDataConfig.hpPer) { //百分百的生命 hpPer
                        let hpPer = utils.getIncValues(triggerEventDataConfig.hpPer, triggerEffectData.effect.eSkillLvl)
                        value = hpPer * fighter.battleAttris.maxHp / 100
                        if (trigger.unitType == UnitType.Altar) {
                            let { totalAttack } = this.battleHandle.getAltarAttack(trigger.teamID)
                            if (value >= totalAttack * 2) {
                                value = totalAttack * 2
                            }
                        } else {
                            if (value >= trigger.battleAttris.attack * 10) {
                                value = trigger.battleAttris.attack * 10
                            }
                        }
                    } else if (triggerEventDataConfig.attackPer) { //攻击的等量生命 attackPer
                        let curePer = utils.getIncValues(triggerEventDataConfig.attackPer, triggerEffectData.effect.eSkillLvl)
                        value = Math.floor(curePer * trigger.battleAttris.attack / 100)
                    }
                    value = Math.floor(value)
                    let damageData = new DamageDataModel({
                        ...triggerEffectData,
                        damage: value,
                        hurtType: HurtType.Null,
                        attacker: trigger,
                        decHpName: triggerEventDataConfig.decHpName || '',
                    })
                    await this.battleHandle.decHp(fighter, trigger, damageData)

                }
            } else if (triggerEventDataConfig.attriName == 'anger') { //治疗怒气
                for (let i = 0; i < partners.length; i++) {
                    let fighter = partners[i]
                    if (fighter.battleAttris.hp >= 1) {
                        let cureValue = utils.getIncValues(triggerEventDataConfig.value, triggerEffectData.effect.eSkillLvl)
                        fighter.battleAttris.anger -= cureValue
                        if (fighter.battleAttris.anger < 0) {
                            fighter.battleAttris.anger = 0
                        }
                        this.battleHandle.recoverAnimation(fighter.teamIndexID, -cureValue, AnimationValueType.Anger)
                    }
                }
            } else if (triggerEventDataConfig.attriName == 'shield') { //护盾
                for (let i = 0; i < partners.length; i++) {
                    let fighter = partners[i]
                    if (fighter.battleAttris.hp >= 1) {
                        let cureValue = 0
                        if (triggerEventDataConfig.value) { //治疗值 value
                            cureValue = utils.getIncValues(triggerEventDataConfig.value, triggerEffectData.effect.eSkillLvl)
                        } else if (triggerEventDataConfig.hpPer) { //治疗百分百的生命 hpPer
                            let curePer = utils.getIncValues(triggerEventDataConfig.hpPer, triggerEffectData.effect.eSkillLvl)
                            cureValue = curePer * trigger.battleAttris.maxHp / 100
                        } else if (triggerEventDataConfig.attackPer) { //治疗攻击的等量生命 attackPer
                            let curePer = utils.getIncValues(triggerEventDataConfig.attackPer, triggerEffectData.effect.eSkillLvl)
                            cureValue = Math.floor(curePer * trigger.battleAttris.attack / 100)
                        }
                        cureValue = Math.floor(cureValue)
                        fighter.battleAttris.shield -= cureValue
                        if (fighter.battleAttris.shield < 0) {
                            fighter.battleAttris.shield = 0
                        }
                        this.battleHandle.recoverAnimation(fighter.teamIndexID, -cureValue, AnimationValueType.CureShield)
                    }
                }
            }

        } else if (triggerEventDataConfig.eventName == "blood") { //吸血
            let bloodPer = utils.getIncValues(triggerEventDataConfig.bloodPer, triggerEffectData.effect.eSkillLvl)
            let hp = Math.floor(triggerEffectData.damage * bloodPer / 100)
            this.battleHandle.addHp(trigger, hp, AnimationValueType.BloodHp)
        } else if (triggerEventDataConfig.eventName == "clearBuff") { //清除buff
            // console.log('开始清除buff', triggerEventDataConfig)
            let amount = triggerEventDataConfig.targetAmount ? utils.getIncValues(triggerEventDataConfig.targetAmount, triggerEffectData.effect.eSkillLvl) : 0
            let partners = this.battleHandle.getFightersByTargetType(trigger, triggerEventDataConfig.targetType, amount, triggerEventDataConfig.targetRankType, stakeholder)
            partners.forEach(partner => {
                let buffs = partner.buffs
                for (let i = buffs.length - 1; i >= 0; i--) {
                    let buff = buffs[i]
                    // console.log(propName[partner.tid], partner.teamID, propName[buff.tid])
                    let buffConfig = gameConfigHandle.configByID[buff.tid]
                    if (triggerEventDataConfig.buffPositive && triggerEventDataConfig.buffPositive != buffConfig.positive) {
                        continue
                    }
                    if (triggerEventDataConfig.buffMainType && triggerEventDataConfig.buffMainType != BuffMainType.All
                        && triggerEventDataConfig.buffMainType != buffConfig.buffMainType) {
                        continue
                    }
                    if (triggerEventDataConfig.buffType && triggerEventDataConfig.buffType != BuffType.None
                        && triggerEventDataConfig.buffType != buffConfig.buffType) {
                        continue
                    }
                    // console.log('已经清除buff', propName[partner.tid], propName[buff.tid])
                    buffs.splice(i, 1)
                }
                fighterHandle.calcBattleFighterAttri(partner, this.battleHandle.aureoles)
            })

        } else if (triggerEventDataConfig.eventName == "addSkillAttris") { //增加技能属性
            utils.entries(triggerEventDataConfig.data).forEach(attriItem => {
                let name = attriItem[0]
                let values: any = attriItem[1]
                triggerEffectData.skill[name] += utils.getIncValues(values, triggerEffectData.effect.eSkillLvl)
            })

        }
    }

    async addBuff(deliver: FighterModel, affecteder: FighterModel, buffID: number, buffLvl: number, tempID: number, triggerEventDataConfig, triggerEffectData: TriggerEffectDataModel) {
        let { round, isStack, buffAttris, roundHurt } = triggerEventDataConfig
        let buffConfig = gameConfigHandle.configByID[buffID]
        let buff = new BuffModel({
            tid: buffID,
            tempID: tempID,
            lvl: buffLvl,
            round: round,
            roundIndex: this.battleHandle.round,
            deliverIndex: deliver ? deliver.teamIndexID : 0,
            attris: buffAttris ? utils.assign({}, buffAttris) : {},
            roundHurt: roundHurt,
            stack: 1,
        })
        if (isStack == true || isStack == undefined || isStack == null) {
            if (round == 0) {
                let index = affecteder.buffs.findIndex(item => item.tempID == tempID)
                if (index !== -1) {
                    affecteder.buffs[index].stack += 1
                } else {
                    affecteder.buffs.push(buff)
                }
            } else {
                affecteder.buffs.push(buff)
            }
        } else {
            let index = affecteder.buffs.findIndex(item => item.tempID == tempID)
            if (index !== -1) {
                affecteder.buffs[index] = buff
            } else {
                affecteder.buffs.push(buff)
            }
        }

        fighterHandle.calcBattleFighterAttri(affecteder, this.battleHandle.aureoles)

        if (buffConfig.buffType !== BuffType.Attri && buffConfig.positive == BuffPositive.Negative) {
            await this.triggerEffects(affecteder, TriggerType.AddNegativeStatus, deliver, new TriggerEffectDataModel({ ...triggerEffectData, buffType: buffConfig.buffType }))
            await this.triggerEffects(null, TriggerType.OtherGetNegativeStatus, affecteder, new TriggerEffectDataModel({ ...triggerEffectData, buffType: buffConfig.buffType }))
        }
        this.battleHandle.fighterNodeUpdate(affecteder.teamIndexID, buff)
    }

    clearBuffBybuffID(fighter: FighterModel, buffID: number) {
        let index = fighter.buffs.findIndex(buff => {
            if (buff.tid == buffID) {
                return true
            }
        })
        if (index !== -1) {
            let buff = fighter.buffs[index]
            fighter.buffs.splice(index, 1)
            if (buff.timerID !== 0) {
                this.battleHandle.timerHandle.clear(buff.timerID)
                let index = fighter.timers.findIndex(timerID => timerID == buff.timerID)
                if (index !== -1) {
                    fighter.timers.splice(index, 1)
                }
            }
            fighterHandle.calcBattleFighterAttri(fighter)
            this.battleHandle.fighterNodeUpdate(fighter.teamIndexID)
        }
    }

    skillAddBuff(deliver: FighterModel, skill: FighterSkillModel, buffID: number, buffLvl: number, addRound: number = 0, addStack: number = 0) {
        let buffConfig = gameConfigHandle.configByID[buffID]
        let index = skill.buffs.findIndex(item => item.tid == buffID)
        let maxStack = buffConfig.stack == 0 ? 0 : utils.getIncValues(buffConfig.stack, buffLvl - 1)
        let rounds = buffConfig.stack ? 0 : utils.getIncValues(buffConfig.Rounds, buffLvl - 1) + addRound
        let buff = new BuffModel({
            tid: buffID,
            lvl: buffLvl,
            round: rounds,
            stack: buffConfig.stack == 0 ? 0 : (addStack || 1),
            deliverIndex: deliver.teamIndexID,
        })

        if (index == -1) {
            skill.buffs.push(buff)
        } else {
            if (maxStack > 0) {
                skill.buffs[index].stack += addStack + 1
                if (skill.buffs[index].stack >= maxStack) {
                    skill.buffs[index].stack = maxStack
                }
            } else {
                skill.buffs[index] = buff
            }
        }
        this.battleHandle.fighterNodeUpdate(deliver.teamIndexID)
    }

    addAureole(deliver: FighterModel, buffID: number, buffLvl: number, teamID: TeamID) {
        let buff = new BuffModel({
            buffID: buffID,
            lvl: buffLvl,
            deliverIndex: deliver.teamIndexID,
            teamID: teamID
        })
        let index = this.battleHandle.aureoles.findIndex(item => {
            if (item.tid == buffID && item.teamID == teamID) {
                return true
            }
        })
        if (index == -1) {
            this.battleHandle.aureoles.push(buff)
            let team = this.battleHandle.getTeam(deliver.teamID)
            team.forEach(fighter => {
                if (fighter) {
                    fighterHandle.calcBattleFighterAttri(fighter, this.battleHandle.aureoles)
                }
            })
        }
    }
    clearDeliverAureole(deliverIndexID: number) {
        let flag = false
        let aureoles = this.battleHandle.aureoles
        for (let j = aureoles.length - 1; j >= 0; j++) {
            let aureole = aureoles[j]
            if (aureole.deliverIndex == deliverIndexID) {
                this.battleHandle.aureoles.splice(j, 1)
                flag = true
                let deliver = this.battleHandle.getFighterByTeamIndexID(deliverIndexID)
                let team = this.battleHandle.getTeam(deliver.teamID)
                team.forEach(fighter => {
                    if (fighter) {
                        fighterHandle.calcBattleFighterAttri(fighter, this.battleHandle.aureoles)
                    }
                })
            }
        }
    }
    clearBuffID(deliver: FighterModel, buffID: number) {
        let buffs = deliver.buffs
        let flag = false
        for (let i = buffs.length - 1; i >= 0; i--) {
            let buff = buffs[i];
            if (buff.tid == buffID) {
                buffs.splice(i, 1)
                flag = true
                break
            }
        }
        if (flag) {
            fighterHandle.calcBattleFighterAttri(deliver, this.battleHandle.aureoles)
        }
    }
    decBuffStack(deliver: FighterModel, buffID: number, decStack: number) {
        let buffs = deliver.buffs
        let flag = false
        for (let i = buffs.length - 1; i >= 0; i--) {
            let buff = buffs[i];
            if (buff.tid == buffID) {
                buff.stack -= decStack
                if (buff.stack <= 0) {
                    buffs.splice(i, 1)
                }
                flag = true
                break
            }
        }
        if (flag) {
            fighterHandle.calcBattleFighterAttri(deliver, this.battleHandle.aureoles)
        }
    }
    clearOneBuff(deliver: FighterModel) {
        let buffs = deliver.buffs
        let buffIndexs = []
        for (let i = buffs.length - 1; i >= 0; i--) {
            let buff = buffs[i];
            let buffConfig = gameConfigHandle.configByID[buff.tid]
            if (buffConfig.positive == 1) {
                buffIndexs.push(i)
            }
        }
        if (buffIndexs.length > 0) {
            let index = utils.getOneFromArray(buffIndexs, this.battleHandle.rand)
            buffs.splice(index, 1)
            fighterHandle.calcBattleFighterAttri(deliver, this.battleHandle.aureoles)
            this.battleHandle.fighterNodeUpdate(deliver.teamIndexID)
        }
    }
    clearOneDeBuff(deliver: FighterModel) {
        let buffs = deliver.buffs
        let buffIndexs = []
        for (let i = buffs.length - 1; i >= 0; i--) {
            let buff = buffs[i];
            let buffConfig = gameConfigHandle.configByID[buff.tid]
            if (buffConfig.positive == 2) {
                buffIndexs.push(i)
            }
        }
        if (buffIndexs.length > 0) {
            let index = utils.getOneFromArray(buffIndexs, this.battleHandle.rand)
            buffs.splice(index, 1)
            fighterHandle.calcBattleFighterAttri(deliver, this.battleHandle.aureoles)
            this.battleHandle.fighterNodeUpdate(deliver.teamIndexID)
        }

    }
    clearOneBuffByType(deliver: FighterModel, type: BuffType) {
        let buffs = deliver.buffs
        for (let i = buffs.length - 1; i >= 0; i--) {
            let buff = buffs[i];
            let buffConfig = gameConfigHandle.configByID[buff.tid]
            if (buffConfig.buffType == type) {
                buffs.splice(i, 1)
                break
            }
        }
        fighterHandle.calcBattleFighterAttri(deliver, this.battleHandle.aureoles)
        this.battleHandle.fighterNodeUpdate(deliver.teamIndexID)
    }

    getDeBuff(deliver: FighterModel): BuffModel[] {
        let buffs = deliver.buffs
        let debuffs: BuffModel[] = []
        for (let i = buffs.length - 1; i >= 0; i--) {
            let buff = buffs[i];
            let buffConfig = gameConfigHandle.configByID[buff.tid]
            if (buffConfig.positive == 2) {
                debuffs.push(buff)
            }
        }
        return debuffs
    }

    getDeBuffHurt(fighter: FighterModel, deBuffAHurt: number, deBuffSHurt: number): number {
        let buffs = fighter.buffs
        let amount = 0, stack = 0
        for (let i = buffs.length - 1; i >= 0; i--) {
            let buff = buffs[i];
            let buffConfig = gameConfigHandle.configByID[buff.tid]
            if (buffConfig.positive == 2) {
                amount += 1
                if (buffConfig.stack) {
                    stack += buff.stack
                }
            }
        }
        return amount * deBuffAHurt + deBuffSHurt * stack
    }



}